﻿using System;
using System.Collections.Generic;

namespace ExcelConfig.helpers
{
    internal partial class helper
    {
        public static string calc_excel_cellname(int rowno, int colno)
        {
            var colstack= new Stack<char>();

            var i = colno % 26;
            colstack.Push(read_abc(i));
            colno -= i;
            while (colno > 0)
            {
                colno /= 26;
                i = colno % 26;
                colstack.Push(read_abc(i-1));
                colno -= i;
            }
            var cellname = string.Empty;
            while (colstack.Count > 0) cellname += colstack.Pop();

            cellname += (rowno+1);
            return cellname;
        }
        public static int readcolno(string cellno)
        {
            var colno = string.Empty;
            var queue = new Stack<byte>();
            foreach (var c in cellno)
                if (c >= 'a' && c <= 'z') queue.Push((byte)(c - 96));
                else if (c >= 'A' && c <= 'Z') queue.Push((byte)(c - 64));

            var no = 0;
            var base_ = 1;
            while (queue.Count > 0)
            {
                no += base_ * queue.Pop();
                base_ *= 26;
            }
            return no;
        }

        internal static char read_abc(int no)
        {
            return (char)('A' + no);
        }
    }
}
